package com.tshopping.service.impl;

import com.tshopping.bean.Page;
import com.tshopping.bean.User;
import com.tshopping.dao.UserDao;
import com.tshopping.service.UserService;
import com.tshopping.util.FileUploader;
import com.tshopping.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @name: AddressServiceImpl
 * @description: 用户业务层类
 * @author: Jackeys
 * @date: 2020-12-24 17:36
 */
@Service(value = "userService")
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    /**
     * @description 处理用户登录
     * @Param: email
     * @Param: password
     * @return User
     * @author Jackeys
     * @date 2020-12-13 11:53
     */
    public User login(String email, String password) {
        return userDao.findUserByEmailAndPassword(email, password);
    }

    @Override
    /**
     * @description 处理用户注册
     * @Param: email
     * @Param: password
     * @return boolean
     * @author Jackeys
     * @date 2020-12-13 11:54
     */
    public boolean register(String email, String password) {
        return userDao.insertUser(email, password);
    }

    @Override
    /**
     * @description 处理用户忘记密码
     * @Param: email
     * @Param: password
     * @return boolean
     * @author Jackeys
     * @date 2020-12-13 12:00
     */
    public boolean forgetPassword(String email, String password) {
        return userDao.updateUserPasswordByEmail(email, password);
    }

    @Override
    /**
     * @description 查询账号是否被注册过
     * @Param: email
     * @return int
     * @author Jackeys
     * @date 2020-12-17 18:25
     */
    public int isExist(String email) {
        return userDao.findUserCountByEmail(email);
    }

    @Override
    /**
     * @description 修改密码
     * @Param: newPassword
     * @Param: password
     * @Param: email
     * @return ResponseData
     * @author Jackeys
     * @date 2020-12-19 14:34
     */
    public ResponseData updatePassword(String newPassword, String oldPassword, String email) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag", userDao.updateUserPasswordByOldPassword(newPassword, oldPassword, email));
        return responseData;
    }

    @Override
    /**
     * @description 修改用户信息
     * @Param: screenName
     * @Param: name
     * @Param: sex
     * @Param: phone
     * @Param: email
     * @return ResponseData
     * @author Jackeys
     * @date 2020-12-24 21:29
     */
    public ResponseData updateInformation(String screenName, String name, String sex, String phone, String email, String headPortrait) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag", userDao.updateUserByEmail(screenName, name, sex, phone, email, headPortrait));
        return responseData;
    }

    @Override
    /**
     * @description 上传用户头像
     * @Param: email
     * @Param: headPortrait
     * @return boolean
     * @author Jackeys
     * @date 2021-01-03 12:07
     */
    public ResponseData uploadHeadPortrait(HttpServletRequest request, String email, MultipartFile file) {
        ResponseData responseData = ResponseData.ok();
        String directoryPath = request.getSession().getServletContext().getRealPath("resource/users/" + email + "/headPortrait/");
        String originalFileName = file.getOriginalFilename();
        String newFileName = UUID.randomUUID() + originalFileName;
        String path = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/resource/users/" + email + "/headPortrait/" + newFileName;
        if (FileUploader.upload(directoryPath, newFileName, file)) {
            responseData.putDataValue("pictureURL", path);
        } else {
            responseData.putDataValue("pictureURL", null);
        }
        return responseData;
    }

    @Override
    /**
     * @description 处理用户充值
     * @Param: account
     * @Param: user
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-31 00:33
     */
    public ResponseData addAccount(int account, int user) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag",userDao.addAccountById(account,user));
        return responseData;
    }

    @Override
    /**
     * @description 返回用户信息
     * @Param: email
     * @Param: password
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-31 01:07
     */
    public ResponseData getUser(String email, String password) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("user",userDao.findUserByEmailAndPassword(email,password));
        return responseData;
    }

    @Override
    public ResponseData getAllUser(int currentPage) {
        ResponseData responseData = ResponseData.ok();
        Page page = new Page();
        //1、当前页数
        page.setCurrentPage(currentPage);

        //2、每页显示的数据
        int pageSize = 5;
        page.setPageSize(pageSize);

        //3、总记录数
        int totalCount = userDao.selectUserCount();
        page.setTotalCount(totalCount);

        //4、根据总记录数和每页显示的记录数计算总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        page.setTotalPage(num.intValue());

        int start = (currentPage - 1) * pageSize;
        int size = pageSize;
        responseData.putDataValue("page", page);
        responseData.putDataValue("userList", userDao.selectAllUser(start,size));
        return responseData;
    }

    @Override
    /**
     * @description 用户封停
     * @Param: isValid
     * @Param: id
     * @return  ResponseData
     * @author Jackeys
     * @date 2021-02-01 15:05
     */
    public ResponseData updateIsValid(int isValid, int id) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag",userDao.updateUserIsValid(isValid,id));
        return responseData;
    }

    @Override
    /**
     * @description 删除用户
     * @Param: id
     * @return ResponseData
     * @author Jackeys
     * @date 2021-02-01 15:05
     */
    public ResponseData deleteUser(int id) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag",userDao.deleteUserById(id));
        return responseData;
    }
}
